package com.future.dao;

import java.sql.Connection;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.future.model.po.ThirdFillBlankModel;
import com.future.model.po.UserModel;

public class ThirdFillBlankDAO extends BaseDAO<ThirdFillBlankModel> {

	public ThirdFillBlankDAO() {

	}

	public ThirdFillBlankDAO(Connection conn){
		super(conn);
	}

	private static Logger logger = LogManager.getLogger(ThirdFillBlankDAO.class);

	public boolean saveThirdFillBlank(ThirdFillBlankModel model) throws Exception {
		String sql = "insert into thirdfillblank ( userid, item0101 , item0201 , item0202, item0301, item0401) values (?,?,?,?,?,?)";
		logger.info("sql=" + sql);
		if (model.getUserid() == null || ("".equals(model.getUserid()))) {
			throw new Exception("用户id为空");
		}
		return this.update(sql, model.getUserid(), model.getItem0101(), model.getItem0201(), model.getItem0202(),
				model.getItem0301(), model.getItem0401());
	}

	public ThirdFillBlankModel findByUserid(UserModel userModel) {
		ThirdFillBlankModel blankModel = null;
		StringBuffer sql = new StringBuffer();
		sql.append(" select * from thirdfillblank where 1=1 ");
		if (userModel != null && userModel.getId() != null && userModel.getId() > 0) {
			sql.append(" and userid=? ");
			logger.info("填空sql=" + sql);
			List<ThirdFillBlankModel> blankLists = this.searchAsBeanList(sql.toString(), ThirdFillBlankModel.class,
					userModel.getId());
			if (blankLists != null && blankLists.size() > 0) {
				blankModel = blankLists.get(0);
			}
		}
		return blankModel;
	}

	/**
	 * 判断第三部分的题是否做过，如果做过返回true , 没做过返回 true
	 * 
	 * @param fillBlankModel
	 * @return
	 */
	public boolean isOrExist(ThirdFillBlankModel fillBlankModel) {
		boolean flag = false;
		String sql = " select * from thirdfillblank where userid=?";
		logger.info("判断第三部分的题是否做过sql=" + sql);
		List<ThirdFillBlankModel> list = this.searchAsBeanList(sql, ThirdFillBlankModel.class,
				fillBlankModel.getUserid());
		if (list != null && list.size() > 0) {
			flag = true;
		} else {
			flag = false;
		}
		return flag;
	}

	/**
	 * 修改第三部分填空题
	 * 
	 * @param fillBlankModel
	 * @return
	 */
	public boolean updateThirdFillBlank(ThirdFillBlankModel fillBlankModel) {
		StringBuffer sb = new StringBuffer();
		sb.append(" update thirdfillblank set item0101=?,item0201=?,item0202=?, ").append(
				" item0301=?,item0401=? where userid=?");
		logger.info(" 修改第三部分填空题sql=" + sb.toString());
		return this.update(sb.toString(), fillBlankModel.getItem0101(),
				fillBlankModel.getItem0201(), fillBlankModel.getItem0202(), fillBlankModel.getItem0301(),
				fillBlankModel.getItem0401(), fillBlankModel.getUserid());
	}

}
